package com.zhuoan.im.webapp.biz.impl;

import com.alibaba.fastjson.JSONObject;
import com.zhuoan.im.webapp.biz.GroupMemberBiz;
import com.zhuoan.im.webapp.biz.UserBiz;
import com.zhuoan.im.webapp.biz.UserGroupBiz;
import com.zhuoan.im.webapp.dao.mapper.GroupsMapper;
import com.zhuoan.im.webapp.dao.mapper.UserGroupMapper;
import com.zhuoan.im.webapp.dao.model.Groups;
import com.zhuoan.im.webapp.dao.model.UserGroup;
import com.zhuoan.im.webapp.dao.model.vo.UserVO;
import org.jim.common.packets.Group;
import org.jim.common.packets.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * UserGroupBizImpl
 *
 * @author weixiang.wu
 * @date 2018-10-15 17:03
 **/
@Service
public class UserGroupBizImpl implements UserGroupBiz {

    private final static Logger logger = LoggerFactory.getLogger(UserGroupBizImpl.class);

    @Resource
    private UserGroupMapper userGroupMapper;

    @Resource
    private GroupsMapper groupsMapper;

    @Resource
    private GroupMemberBiz groupMemberBiz;

    @Resource
    private UserBiz userBiz;

    @Override
    public List<UserGroup> queryUserGroupByCreatorUserId(Long id) {
        return userGroupMapper.queryUserGroupByCreatorUserId(id);
    }

    @Override
    public int insertSelective(UserGroup record) {
        return userGroupMapper.insertSelective(record);
    }

    @Override
    public UserGroup queryGroupByCreatorUserIdAndType(Long creatorUserId, Integer myFriends) {
        return userGroupMapper.queryGroupByCreatorUserIdAndType(creatorUserId, myFriends);
    }

    @Override
    public Group queryGroupAndInnerUserInfoByGroupId(Long groupId) {
        //获取当前群组内的所有用户的id
        List<Long> inCurrentGroupUserIds = groupMemberBiz.queryInCurrentGroupUserIds(groupId);

        List<org.jim.common.packets.User> myFriendGroupUsers = new ArrayList<>();
        if (inCurrentGroupUserIds.size() > 0) {
            //获取当前群组内的所有用户的信息
            List<UserVO> userVOS = userBiz.queryUserVOByIds(inCurrentGroupUserIds);
            for (UserVO userVO : userVOS) {
                User user = transferJimUserByUserVO(userVO);
                myFriendGroupUsers.add(user);
            }
        }

        //获取jim群组,包含用户信息
        return getJimGroup(groupId, myFriendGroupUsers);
    }

    /**
     * 获取jim群组信息，包含用户信息
     *
     * @param groupId
     * @param myFriendGroupUsers
     * @return
     */
    private Group getJimGroup(Long groupId, List<User> myFriendGroupUsers) {
        Groups groups = groupsMapper.selectByPrimaryKey(groupId);
        Group group = new Group();
        group.setGroup_id(String.valueOf(groupId));
        // 将当前群组内的所有用户的信息放入到 群组里
        group.setUsers(myFriendGroupUsers);
        group.setName(groups.getGroupName());
        group.setAvatar(groups.getGroupAvatar());
        Long online = groups.getOnline();
        group.setOnline(Math.toIntExact(online == null ? 1 : online));
        group.setCreateTime(groups.getGmtCreate().getTime());
        // 扩展字段
        JSONObject extras = new JSONObject();
        extras.put("notice", groups.getNotice());
        extras.put("qrCore", groups.getQrCore());
        group.setExtras(extras);
        return group;
    }


    private User transferJimUserByUserVO(UserVO userVO) {
        User user = new User();
        user.setId(String.valueOf(userVO.getId()));
        user.setNick(userVO.getNick());
        user.setAvatar(userVO.getAvatar());
        user.setSign(userVO.getSign());
        user.setStatus(String.valueOf(userVO.getStatus()));
        // 扩展字段
        JSONObject extras = new JSONObject();
        extras.put("username", userVO.getUsername());
        extras.put("salt", userVO.getSalt());
        extras.put("blockAddr", userVO.getBlockAddr());
        extras.put("referralCode", userVO.getReferralCode());
        extras.put("address", userVO.getAddress());
        extras.put("sex", userVO.getSex());
        extras.put("email", userVO.getEmail());
        extras.put("dateOfBirth", userVO.getDateOfBirth());
        extras.put("phone", userVO.getPhone());
        extras.put("remark", userVO.getRemark());
        extras.put("gmtCreate", userVO.getGmtCreate());
        user.setExtras(extras);
        return user;
    }
}
